#include "mprpcchannel.h"
#include <string>
#include "rpcheader.pb.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <error.h>
#include <unistd.h>
#include "mprpcapplication.h"
void MprpcChannel::CallMethod(const google::protobuf::MethodDescriptor *method,
                             google::protobuf::RpcController *controller,
                             const google::protobuf::Message *request,
                             google::protobuf::Message *response,
                             google::protobuf::Closure *done)
{
    const google::protobuf::ServiceDescriptor *sd = method->service();
    std::string service_name = sd->name();
    std::string method_name = method->name();

    //获取参数的序列化字符串长度 args_size
    int args_size = 0;
    std::string args_str;
    if (request->SerializeToString(&args_str))
    {
        //做rpc调用
        args_size = args_str.size();
    }
    else
    {
        std::cout << "serialize request error!" << std::endl;
    }

    //定义rpc的请求header
    mprpc::RpcHeader rpcHeader;
    rpcHeader.set_service_name(service_name);
    rpcHeader.set_method_name(method_name);
    rpcHeader.set_args_size(args_str.size());

    uint32_t header_size = 0;
    std::string rpc_header_str;
    if(rpcHeader.SerializeToString(&rpc_header_str))
    {
        header_size = rpc_header_str.size();
    }
    else
    {
        std::cout << "serialize rpc header error!" << std::endl;
        return;
    }

    //组织待发送的rpc请求数据
    std::string send_rpc_str;
    send_rpc_str.insert(0, std::string((char *)&header_size, 4));
    send_rpc_str += rpc_header_str;
    send_rpc_str += args_str;

    //打印调试信息
    std::cout << "==========================================" << std::endl;
    std::cout << "header_size: " << header_size << std::endl;
    std::cout << "service_name: " << service_name << std::endl;
    std::cout << "method_name: " << method_name << std::endl;
    std::cout << "args_str: " << args_str << std::endl;

    //使用tcp编程，完成rpc方法的远程调用
    int clientfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == clientfd)
    {
        std::cout << "socket error: " << strerror(errno) << std::endl;
        exit(EXIT_FAILURE);
    }

    std::string ip = MprpcApplication::GetInstance().GetConfig().Load("rpcserverip");
    uint16_t port = atoi(MprpcApplication::GetInstance().GetConfig().Load("rpcserverport").c_str());

    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    server_addr.sin_addr.s_addr = inet_addr(ip.c_str());
    
    //连接rpc服务节点
    if(-1 == connect(clientfd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
    {
        std::cout << "connect error: " << strerror(errno) << std::endl;
        close(clientfd);
        exit(EXIT_FAILURE);
    }

    //发送rpc请求
    if(-1 == send(clientfd, send_rpc_str.data(), send_rpc_str.size(), 0))
    {
        std::cout << "send error: " << strerror(errno) << std::endl;
        close(clientfd);
        return;
    }

    //接收rpc请求的响应值
    char recv_rpc_str[1024] = {0};
    int recv_size;
    if (-1 == (recv_size=recv(clientfd, recv_rpc_str, 1024, 0)))
    {
        close(clientfd);
        std::cout << "recv error: " << strerror(errno) << std::endl;
        return;
    }

    std::string response_str(recv_rpc_str,recv_size);
    if(response->ParseFromString(response_str))
    {
        std::cout << "response parse success!" << std::endl;
        close(clientfd);
        return;
    }
    else
    {
        std::cout << "response parse error!" << std::endl;
        close(clientfd);
        return;
    }

    close(clientfd);
}